home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / wmv12s.zip / SECTOR.C < prev    next >
Text File  |  1993-01-04  |  3KB  |  97 lines

  1. /* Routines to read and write sectors
  2. ** Only one copy of a particular sector will be stored, to prevent
  3. ** inconsistency.
  4. ** Written by Peter Wu 7/11/86 @ Faculty Support Center @ Univ. of Wisconsin
  5. ** This is used by the mv (move files and directory) program.
  6. ** Link with absdr (source absdr.asm)
  7. */
  8. #define LINT_ARGS
  9.  
  10. #define  MAX_SECTOR_SIZE  4200    /* is this safe enough? */
  11. #define  MAX_COPIES  5        /* max # of sectors buffered */
  12.  
  13. #include <conio.h>
  14.  
  15. /* global stuff */
  16.  
  17. char secbufx[MAX_SECTOR_SIZE * MAX_COPIES], dirty[MAX_COPIES];
  18. int secind[MAX_COPIES], drvind[MAX_COPIES];
  19. int num_sec;  /* number of sectors currently buffered */
  20.  
  21. char *readsec(drv, sector)  /* read a sector; return pointer to buffer */
  22. int drv, sector;
  23. {
  24.   int i, status;
  25.   char *where;
  26.  
  27. #ifdef debug
  28.   printf("reading sector# %d\n",sector);
  29. #endif
  30.   /* first see if the sector requested is already in buffer */
  31.   for (i=0; i < num_sec; i++) {
  32.     if ((secind[i] == sector) &&
  33.     (drvind[i] == drv)    ) {  /* aha - sector already read */
  34.       return secbufx + i * MAX_SECTOR_SIZE;  /* return pointer to buffer */
  35.     }
  36.   }
  37.  
  38.   /* sector is not in buffer, so read it, but first allocate a buffer */
  39.   secind[num_sec] = sector;  /* store sector number */
  40.   drvind[num_sec] = drv;
  41.   where = secbufx + num_sec * MAX_SECTOR_SIZE;
  42.   status = absdr(drv, 1, sector, where);
  43.   if (status) {
  44.     cputs("Error in readsec calling absdr\n\015");
  45.     return (char *) 0;
  46.   }
  47.   dirty[num_sec] = 0;  /* not dirty */
  48.   num_sec++;
  49.  
  50.   /* successfully read a sector into buffer */
  51.   return where;
  52. }
  53.  
  54. writesec(drv,sector)
  55. int drv, sector;
  56. {
  57.   int i, status;
  58.  
  59. #ifdef debug
  60.   printf("writing sector# %d\n", sector);
  61. #endif
  62.   for (i=0; i < num_sec; i++) {
  63.     if ((secind[i] == sector) && (drvind[i] == drv)) {
  64.       if (dirty[i]) {  /* write the sector only if it's dirty */
  65.     status = absdw(drvind[i], 1, secind[i], secbufx + i * MAX_SECTOR_SIZE);
  66.     if (status) {
  67.       cputs("Error in flushsec calling absdw\n\015");
  68.       return -1;
  69.     } else {
  70.       dirty[i] = 0;
  71.     }
  72.       }
  73.       return 0;
  74.     }
  75.   }  /* for */
  76.   cputs("writesec: sector is not in buffer\n\015");
  77.   return -2;
  78. }
  79.  
  80. flirt(drv,sector)  /* turn on dirty flag */
  81. int drv,sector;
  82. {
  83.   int i;
  84.  
  85. #ifdef debug
  86.   printf("flirting sector# %d; num_sec = %d\n",sector,num_sec);
  87. #endif
  88.   for (i=0; i < num_sec; i++) {
  89.     if ((secind[i] == sector) && (drvind[i] == drv)) {
  90.       dirty[i] = 1;
  91.       return 0;
  92.     }
  93.   }
  94.   cputs("flirt: can't find sector to flirt with\n\015");
  95.   return -1;
  96. }
  97.